package com.yanqu.road.dao.impl.activity.yqhotel;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.yqhotel.config.*;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class YQHotelConfigDaoImpl extends TempDao {
    private static String getYQHotelBeastConfigSql = "select * from `t_s_activity_yqhotel_beast` where activityId= ?";
    public Map<Integer, YqHotelBeastConfig> getYQHotelBeastConfigMap(int activityId) {
        Map<Integer, YqHotelBeastConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getYQHotelBeastConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                YqHotelBeastConfig yQHotelBeastConfig = new YqHotelBeastConfig();
                yQHotelBeastConfig.setActivityId(rs.getInt("activityId"));
                yQHotelBeastConfig.setId(rs.getInt("id"));
                yQHotelBeastConfig.setIcon(rs.getInt("icon"));
                yQHotelBeastConfig.setName(rs.getString("name"));
                yQHotelBeastConfig.setDesc(rs.getString("desc"));
                yQHotelBeastConfig.setSourPower(rs.getString("sourPower"));
                yQHotelBeastConfig.setRewards(rs.getString("rewards"));
                map.put(yQHotelBeastConfig.getId(), yQHotelBeastConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }


    private static String getYQHotelCounterConfigSql = "select * from `t_s_activity_yqhotel_counter` where activityId= ?";
    public Map<Integer, Map<Integer, YqHotelCounterConfig>> getYQHotelCounterConfigMap(int activityId) {
        Map<Integer, Map<Integer, YqHotelCounterConfig>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getYQHotelCounterConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                YqHotelCounterConfig yQHotelCounterConfig = new YqHotelCounterConfig();
                yQHotelCounterConfig.setActivityId(rs.getInt("activityId"));
                yQHotelCounterConfig.setType(rs.getInt("type"));
                yQHotelCounterConfig.setLevel(rs.getInt("level"));
                yQHotelCounterConfig.setNum(rs.getInt("num"));
                yQHotelCounterConfig.setConsume(rs.getInt("consume"));
                if (!map.containsKey(yQHotelCounterConfig.getType())) {
                    map.put(yQHotelCounterConfig.getType(), new ConcurrentHashMap<>());
                }
                map.get(yQHotelCounterConfig.getType()).put(yQHotelCounterConfig.getLevel(), yQHotelCounterConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getYQHotelEventConfigSql = "select * from `t_s_activity_yqhotel_event` where activityId= ?";
    public Map<Integer, YqHotelEventConfig> getYQHotelEventConfigMap(int activityId) {
        Map<Integer, YqHotelEventConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getYQHotelEventConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                YqHotelEventConfig yQHotelEventConfig = new YqHotelEventConfig();
                yQHotelEventConfig.setActivityId(rs.getInt("activityId"));
                yQHotelEventConfig.setId(rs.getInt("id"));
                yQHotelEventConfig.setIcon(rs.getString("icon"));
                yQHotelEventConfig.setRewards(rs.getString("rewards"));
                yQHotelEventConfig.setDesc(rs.getString("desc"));
                yQHotelEventConfig.setName(rs.getString("name"));

                map.put(yQHotelEventConfig.getId(), yQHotelEventConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }


    private static String getYQHotelSkillConfigSql = "select * from `t_s_activity_yqhotel_skill` where activityId= ?";
    public Map<Integer, Map<Integer, YqHotelSkillConfig>> getYQHotelSkillConfigMap(int activityId) {
        Map<Integer, Map<Integer, YqHotelSkillConfig>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getYQHotelSkillConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                YqHotelSkillConfig yQHotelSkillConfig = new YqHotelSkillConfig();
                yQHotelSkillConfig.setActivityId(rs.getInt("activityId"));
                yQHotelSkillConfig.setType(rs.getInt("type"));
                yQHotelSkillConfig.setLevel(rs.getInt("level"));
                yQHotelSkillConfig.setIcon(rs.getInt("icon"));
                yQHotelSkillConfig.setName(rs.getString("name"));
                yQHotelSkillConfig.setDesc(rs.getString("desc"));
                yQHotelSkillConfig.setCost(rs.getInt("cost"));
                yQHotelSkillConfig.setSkillEffect(rs.getString("skillEffect"));
                yQHotelSkillConfig.setWeight(rs.getInt("weight"));
                if (!map.containsKey(yQHotelSkillConfig.getType())) {
                    map.put(yQHotelSkillConfig.getType(), new ConcurrentHashMap<>());
                }
                map.get(yQHotelSkillConfig.getType()).put(yQHotelSkillConfig.getLevel(), yQHotelSkillConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getYQHotelTitleConfigSql = "select * from `t_s_activity_yqhotel_title` where activityId= ?";
    public Map<Integer, YqHotelTitleConfig> getYQHotelTitleConfigMap(int activityId) {
        Map<Integer, YqHotelTitleConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getYQHotelTitleConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                YqHotelTitleConfig yQHotelTitleConfig = new YqHotelTitleConfig();
                yQHotelTitleConfig.setActivityId(rs.getInt("activityId"));
                yQHotelTitleConfig.setId(rs.getInt("id"));
                yQHotelTitleConfig.setIcon(rs.getInt("icon"));
                yQHotelTitleConfig.setName(rs.getString("name"));
                yQHotelTitleConfig.setDesc(rs.getString("desc"));
                yQHotelTitleConfig.setSourPower(rs.getString("sourPower"));
                yQHotelTitleConfig.setRewards(rs.getString("rewards"));
                yQHotelTitleConfig.setAccelerateTimeCost(rs.getInt("accelerateTimeCost"));
                map.put(yQHotelTitleConfig.getIcon(), yQHotelTitleConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getYqHotelFoodNameConfigSql = "select * from `t_s_activity_yqhotel_foodname` where `activityId`=?";
    public List<YqHotelFoodNameConfig> getYqHotelFoodNameConfigList(int activityId) {
        List<YqHotelFoodNameConfig> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getYqHotelFoodNameConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                YqHotelFoodNameConfig yqHotelFoodNameConfig = new YqHotelFoodNameConfig();
                yqHotelFoodNameConfig.setActivityId(rs.getInt("activityId"));
                yqHotelFoodNameConfig.setId(rs.getInt("id"));
                yqHotelFoodNameConfig.setName(rs.getString("name"));
                list.add(yqHotelFoodNameConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

}
